//
//  RegistViewController.swift
//  Accounting
//
//  Created by Qingke on 16/5/6.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

protocol NamePassDelegate: class{
    func nameDelegate (viewController:RegistViewController , NameID: String)
//    func customIconDelegate(iconImage: UIImage)
}

class RegistViewController: UITableViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    @IBOutlet weak var nameText: UITextField!
    @IBOutlet weak var passwordText: UITextField!
    @IBOutlet weak var tipLabel: UILabel!
    @IBOutlet weak var loginButtonOutlet: UIBarButtonItem!
    @IBOutlet weak var customIcon: UIImageView!
    
    var deleage: NamePassDelegate?
    var NameID: String!
    var customIconPicture: UIImage?
    
    var iconCategory: IconCategory?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        iconCategory = IconCategory()
        nameText.becomeFirstResponder()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 3
    }
    
    //MARK: action button
    
    @IBAction func registButton(sender: UIButton) {
        guard let IDName = nameText.text ,
            let password = passwordText.text else {
                return
        }
        guard !IDName.isEmpty && !password.isEmpty else {
            tipLabel.text = "请输入用户名和密码"
            tipLabel.backgroundColor = UIColor.redColor()
            return
        }
        if let _ = NSUserDefaults.standardUserDefaults().valueForKey(IDName) {
            tipLabel.text = "该用户已存在"
            tipLabel.backgroundColor = UIColor.redColor()
        } else {
            tipLabel.text = "注册成功"
            tipLabel.backgroundColor = UIColor.greenColor()
            loginButtonOutlet.enabled = true
            NameID = IDName
//            NSUserDefaults.standardUserDefaults().setVolatileDomain([IDName: password], forName: "账户名")
            NSUserDefaults.standardUserDefaults().setValue(password, forKey: IDName)
            NSUserDefaults.standardUserDefaults().synchronize()

            
           
        }
    }
    @IBAction func backButton(sender: UIBarButtonItem) {
        dismissViewControllerAnimated(true, completion: nil)
    }
    @IBAction func loginButton(sender: UIBarButtonItem) {

        if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("TotalViewController")  as? TotalViewController {
//            controller.nameDelegate(self, NameID: NameID)
            deleage?.nameDelegate(self, NameID: NameID)//括号相当于新建一个实例化对象
            
            controller.ImageName = customIconPicture
            controller.nameID = NameID
            
            let imageData = UIImageJPEGRepresentation(customIcon.image!, 0.2)
            NSUserDefaults.standardUserDefaults().setValue(imageData, forKey: "customIcon")
            
            
            presentViewController(controller, animated: true, completion: { () -> Void in
            })
        }
    }
    @IBAction func touchImage(sender: UITapGestureRecognizer) {
        nameText.resignFirstResponder()
        let imagePicker = UIImagePickerController()
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        imagePicker.delegate = self
        presentViewController(imagePicker, animated: true, completion: nil)
        
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

        customIconPicture = info["UIImagePickerControllerOriginalImage"] as? UIImage
        
        customIcon.image = customIconPicture
        self.dismissViewControllerAnimated(true, completion: nil)
        
    }
    
   
}
